<template>
	<view class="sendWrapper" @click="onSendTiktok">
		发抖音
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapActions
	} from 'vuex';
	
	const config = require("@/config.js")
	let that = null
	// #ifdef APP-PLUS
	var plugin = uni.requireNativePlugin('Vnidev-DyPlugin-Core')
	const dyclientId = config.tiktokClientKeyApp;
	// #endif
	const api = require("@/static/js/api.js")
	const utils = require("@/static/js/utils.js")
	const typeMappingSharePath = {
		'product': '/pages/client/project/building',
		'house_type': '/pages/client/project/unitTypeDetail',
		'album': '/pages/client/project/buildingAlbumDetail',
		'video': '/pages/client/project/buildingVideoDetail',
		'note': '/pages/client/note/detail',
		'panorama': '/pages/client/web/index?shareParam='
	}
	export default {
		props: {
			id: {
				type: String,
				default: "",
			},
			contentType: {
				type: String,
				default: "",
			},
			sharePath: {
				type: String,
				default: "",
			},
			title: {
				type: String,
				default: "",
			},
		},
		computed: {
			...mapState({
				hasLogin: state => state.userInfo.hasLogin,
				shopInfo: state => state.userInfo.shopInfo,
			}),
		},
		components: {},
		data() {
			return {
				loading: false,
				noteList: [],
			};
		},
		created() {
			that = this
		},



		methods: {
			...mapMutations(['setUserInfo', 'setLoginStatus', 'setUserId', "setOrigin"]),
			async onSendTiktok() {
				if (this.loading){
					return 
				}
				let that = this
				this.loading = true
				var logId = await that.getShareLogIdMixins()
				this.loading = false
				var path = 'pages/home/home?projectId=' + uni.getStorageSync("projectId")
				var logIdPath = '&logId=' + logId
				var shareUri = "&sharePath="
				var uri = this.getSharePath();
				var title = that.title || that.shopInfo.shareInfo;

				if (that.id && that.id.length > 0) {
					if (uri.indexOf('?') == -1) {
						uri += '?id=' + that.id
					} else {
						uri += '&id=' + that.id
					}
				}

				if (uri.indexOf('?') == -1) {
					uri += '?logId=' + logId
				} else {
					uri += '&logId=' + logId
				}

				console.log("uri:", uri)
				shareUri += encodeURIComponent(uri)

				var appUrl = path + logIdPath + shareUri
				this.gotoSendTiktok(title, appUrl)
			},
			getSharePath() {
				if (that.sharePath && that.sharePath.length > 0) {
					return that.sharePath
				}

				if (typeMappingSharePath[this.contentType]) {
					var path = typeMappingSharePath[this.contentType]
					if (this.contentType == 'panorama') {
						path += this.id
						console.log("getSharePath",path)
					}
					
					return path
				}
				return ""
			},


			async getShareLogIdMixins() {
				var res = await api.createUserLog({
					param: {
						actType: 'share',
						contentType: this.contentType,
						contentId: this.id,
						parentId: '',
					}
				})
				console.log(res)
				if (res.success) {
					console.log("shareUserLogMixins:", res.data)
					return res.data
				} else {
					return ''
				}
			},

			gotoSendTiktok(title, appUrl) {
				console.log("gotoSendTiktok", title, appUrl, this.shopInfo.tiktokAppId)
				if (this.shopInfo.tiktokAppId && this.shopInfo.tiktokAppId.length > 0) {			
					// #ifdef APP-PLUS
					plugin.startCreate({
						"appid": dyclientId,
						// "state": "", //a47e57c6c559acb88a9569da66ee5f65e0f779c9
						// "hashtag": "hashtag,hashtag2",
						"mpInfo": {
							"title": title,
							"description": "",
							"appUrl": appUrl,
							"appid": this.shopInfo.tiktokAppId //"tt6b151da8f42b790001"//this.shopInfo.tiktokAppId, //"tt09567feb9666906501"
						},
						// "extraInfoData": {
						// 	"micro_app_task_id": "(long)7263399170818785314",
						// 	"agent_client_key": "aw01dbw55bc3ycge"
						// }
					}, (res) => {
						this.authdata = res
						console.log("onSendTiktok:", res)
					})
					// #endif
				}

			}
		}
	}
</script>

<style lang="scss">
	.sendWrapper {
		height: 50rpx;
		width: 140rpx;
		border-radius: 30rpx;
		background-color: #007aff;
		color: #ffffff;
		font-size: 22rpx;
		text-align: center;
		line-height: 50rpx;
	}
</style>